package smartcounters.androidclient.model;

import java.util.EnumSet;
import java.util.LinkedList;

import org.ksoap2.serialization.SoapObject;

public class UserInfo {
    private int m_ID;
    private String m_Username;
    private EnumSet<eUserPermissions> m_UserPermissions;
    private String m_Phone;
    private String m_MobilePhone;
    private String m_Email;
    private String m_Password;
    
    private LinkedList<Integer> m_AccountsAssociatedWith;
    
    public UserInfo(int i_ID, String i_Username){
    	m_ID = i_ID;
    	m_Username = i_Username;
    	m_AccountsAssociatedWith = new LinkedList<Integer>();
    }
    
    public UserInfo(String i_Username){
    	this(-1, i_Username);
    }
    
    public int getID(){
    	return m_ID;
    }
    
    public void setID(int i_ID){
    	m_ID = i_ID;
    }
    
    public String getUsername(){
    	return m_Username;
    }
    
    public void setUsername(String i_Username){
    	m_Username = i_Username;
    }
    
    public EnumSet<eUserPermissions> getUserPermissions(){
    	return m_UserPermissions;
    }
    
    public void setUserPermissions(EnumSet<eUserPermissions> i_UserPermissions){
    	m_UserPermissions = i_UserPermissions;
    }
    
    public String getPhone(){
    	return m_Phone;
    }
    
    public void setPhone(String i_Phone){
    	m_Phone = i_Phone;
    }
    
    public String getMobilePhone(){
    	return m_MobilePhone;
    }
    
    public void setMobilePhone(String i_MobilePhone){
    	m_MobilePhone = i_MobilePhone;
    }
    
    public String getEmail(){
    	return m_Email;
    }
    
    public void setEmail(String i_Email){
    	m_Email = i_Email;
    }
    
    public String getPassword(){
    	return m_Password;
    }
    
    public void setPassword(String i_Password){
    	m_Password = i_Password;
    }
    
    public LinkedList<Integer> getAccountsAssociatedWith(){
    	return m_AccountsAssociatedWith;
    }
    
    public void setAccountsAssociatedWith(LinkedList<Integer> i_AccountsAssociatedWith){
    	m_AccountsAssociatedWith = i_AccountsAssociatedWith;
    }
    
    public static UserInfo CreateFromSoapObject(SoapObject userInfoObject){
    	UserInfo userInfo = new UserInfo(Integer.parseInt(userInfoObject.getProperty("ID").toString()), userInfoObject.getProperty("Username").toString());
		userInfo.setPhone(userInfoObject.getProperty("Phone").toString());
		userInfo.setMobilePhone(userInfoObject.getProperty("MobilePhone").toString());
		userInfo.setEmail(userInfoObject.getProperty("Email").toString());
		userInfo.setPassword(userInfoObject.getProperty("Password").toString());
		
		String permissionsString = userInfoObject.getProperty("UserPermissions").toString();
		String[] permissionsArray = permissionsString.split(" ");
		
		EnumSet<eUserPermissions> userPermissions = EnumSet.noneOf(eUserPermissions.class);
			
		for (String permission : permissionsArray){
			userPermissions.add(eUserPermissions.valueOf(permission));
		}
		
		userInfo.setUserPermissions(userPermissions);
		
		SoapObject property = (SoapObject)userInfoObject.getProperty("AccountsAssociatedWith");
		LinkedList<Integer> accounts = new LinkedList<Integer>();
		
		for (int i = 0; i < property.getPropertyCount(); i++){
			accounts.add(Integer.parseInt(property.getProperty(i).toString()));
		}
		
		userInfo.setAccountsAssociatedWith(accounts);
		
		return userInfo;
    }
}
